/**
 * kaGrid 封装
 * 
 * 版本：v1.0
 * 
 * Stanley
 **/
layui.define(['jquery'],
    function (exports) {
        var kaGridTopButton = new function () {
            this.init = function (params) { // 初始化
                var toolbar = params.myToolbar;
                var topButtonHtml = ``; // 顶部按钮
                var kjQueryHtml = ``; // 快捷查询
                var querySuoDingHtml = ``; // 锁定查询列表
                var queryHtml = ``; // 查询列表
                var number = 0;
                for (var i in toolbar) {
                    if (toolbar[i].type == 1) {
                        topButtonHtml = topButtonHtml + `<button id="grid_top_button` + toolbar[i].id + `" onclick="top.` + params.buttonFunUrl + `_ButtonFunData.` + toolbar[i].kaEven + `('` + params.id + `','0')" type="button" class="layui-btn layui-btn-sm ` + toolbar[i].myclass + `" style="` + toolbar[i].style + `"><i class="icon Kaultraiconfont ` + toolbar[i].icon + `" style="font-size: 12px;"></i>` + toolbar[i].name + `</button>`;
                    } else if (toolbar[i].type == 2) {
                        var kjQueryDatas = toolbar[i].children;
                        for (var j in kjQueryDatas) {
                            var kjQueryData = kjQueryDatas[j];
                            kjQueryHtml = kjQueryHtml + layui.kaGridTopButton.kjQueryHtml(kjQueryData);
                        }
                    } else if (toolbar[i].type == 3) {
                        var querySuoDingDatas = toolbar[i].children;
                        for (var j in querySuoDingDatas) {
                            var querySuoDingData = querySuoDingDatas[j];
                            // layui-btn-primary layui-border-green
                            querySuoDingHtml = querySuoDingHtml + layui.kaGridTopButton.getQuerySuoDingHtml(querySuoDingData, 1);
                        }
                    } else if (toolbar[i].type == 4) {
                        var queryDatas = toolbar[i].children;
                        for (var j in queryDatas) {
                            var queryData = queryDatas[j];
                            queryHtml = queryHtml + layui.kaGridTopButton.getQueryHtml(queryData);
                            number++;
                        }
                    }
                }
                var queryListShowHidenHtml = ``;
                if (number > 0) {
                    queryHtml = queryHtml + `
                        <div class="layui-col-md3">
                            <div class="layui-form-item" style="margin-bottom: 0;">
                                <button type="button" onclick="layui.kaGridSearch.init('` + params.id + `')" class="layui-btn layui-btn-primary layui-border-blue kaGridTopButtonQueryData">
                                    <i class="icon Kaultraiconfont Kaultraicon-chaxun kaGridTopButtonQueryData_I"></i> 查询
                                </button>
                            </div>
                        </div>
                    `;
                    number = number + 1;
                    queryListShowHidenHtml = queryListShowHidenHtml + `
                        <span onclick="layui.kaGridTopButton.queryListShowHiden('` + params.id + `','` + number + `')" class="queryListShowHidenHtml">
                            <span id="` + params.id + `_text">展开查询</span> <i class="layui-icon layui-font-12 layui-icon-down"></i>
                        </span>
                    `;
                } else {
                    queryListShowHidenHtml = queryListShowHidenHtml + `
                        <span onclick="layui.kaGridTopButton.queryListShowHidenNo('` + params.id + `','` + number + `')" class="queryListShowHidenHtml">
                            <span id="` + params.id + `_text">添加查询</span>
                        </span>
                    `;
                }
                //layui-border layui-border-blue
                kjQueryHtml = `
                    <button type="button" onclick="layui.kaGridTopButton.kjQueryClick(1, 'KAALL',this)" class="kjQueryClass layui-btn layui-btn-xs layui-btn-primary layui-border-blue">
                        全部
                    </button>
                ` + kjQueryHtml;
                return `<div>
                            <div style="height: 38px;line-height: 38px;">
                                ` + topButtonHtml + `
                            </div>
                            <div style="height: 35px;line-height: 35px;"> 
                                ` + queryListShowHidenHtml + `
                               <span>
                                   ` + kjQueryHtml + `
                               </span>
                            </div>
                            <div id="` + params.id + `_content" class="kaGridTopFindQuery" style="display: none;">
                               <form class="layui-form layui-row layui-form-pane layui-col-space16" lay-filter="` + params.id + `_filter">
                                    <div id="querySuoDing_` + params.id + `" class="layui-col-md12" style="height: 30px;line-height: 30px;padding: 0px;padding-left: 8px;">
                                        <span style="background: #e5e5e5;padding: 4px;color: var(--global-primary-color);">查询条件</span>
                                        ` + querySuoDingHtml + `
                                    </div>
                                    ` + queryHtml + `
                               </form>
                            </div>
                        </div>`
            }
            this.judge = function (params) { // 判断
                if (params.myToolbar != undefined && params.myToolbar != null && params.myToolbar.length > 0) {
                    return true;
                } else {
                    return false;
                }
            }
            this.queryListShowHidenNo = function (id, number) {
                top.layer.msg('请点击右侧添加查询条件', { icon: 0 });
            }
            this.queryListShowHidenStart = function (id, number, type) {
                var $ = layui.$;
                var text = $("#" + id + "_text").html();
                if (text == "展开查询" && type == 1) {
                    $("#" + id + "_text").html("隐藏查询");
                    var height = "";
                    if (number <= 4) {
                        height = "80px";
                    }
                    if (number > 4) {
                        height = "125px";
                    }
                    $("#" + id + "_content").attr("style", "height: " + height + ";line-height: " + height + ";");
                } else if (type == 2) {
                    $("#" + id + "_text").html("展开查询");
                    $("#" + id + "_content").attr("style", "display: none;");
                }
            }
            this.queryListShowHiden = function (id, number) {
                var $ = layui.$;
                var text = $("#" + id + "_text").html();
                if (text == "展开查询") {
                    $("#" + id + "_text").html("隐藏查询");
                    var height = "";
                    if (number <= 4) {
                        height = "80px";
                    }
                    if (number > 4) {
                        height = "125px";
                    }
                    $("#" + id + "_content").attr("style", "height: " + height + ";line-height: " + height + ";");
                } else {
                    $("#" + id + "_text").html("展开查询");
                    $("#" + id + "_content").attr("style", "display: none;");
                }
            }
            this.kjQueryHtml = function (data) {
                var html = `
                    <button onclick="layui.kaGridTopButton.kjQueryClick(1, '` + data.id + `',this)" type="button" class="kjQueryClass layui-btn layui-btn-xs layui-btn-primary">
                        <span>` + data.name + `</span>
                    </button>
                `;
                return html;
            }
            this.kjQueryClick = function (type, id, that) {
                var $ = layui.$;
                if (type == 1) {
                    $(".kjQueryClass").removeClass("layui-border");
                    $(".kjQueryClass").removeClass("layui-border-blue");
                    $(".kjQueryClass").addClass("layui-border");
                    if (id == "KAALL") {
                        $("button[QuerySuoDing='2']").remove();
                        $(that).addClass("layui-border-blue");
                        layui.kaGridSearch.init('kaDataGridId');
                    } else {
                        $(that).addClass("layui-border-blue");
                        layui.kaAjax.ajax(layui.kaBgUrl.url("getQuerySuoDing"), function (rs) {
                            var $ = layui.$;
                            if (rs.code == 200) {
                                var querySuoDingDatas = rs.data;
                                $(".QuerySuoDing_2").remove();
                                for (var i in querySuoDingDatas) {
                                    var querySuoDingData = querySuoDingDatas[i];
                                    var buttonId = "QuerySuoDing_" + querySuoDingData.field + "_" + querySuoDingData.judge;
                                    var querySuoDingHtml = layui.kaGridTopButton.getQuerySuoDingHtml(querySuoDingData, 2);
                                    if ($("#" + buttonId).length) {
                                    } else {
                                        $("#querySuoDing_" + querySuoDingData.tableId).append(querySuoDingHtml)
                                    }
                                }
                                layui.kaGridSearch.init('kaDataGridId');
                            }
                        }, { id: id })
                    }
                } else if (type == 2) {
                    $(".QuerySuoDing_3").remove();
                    var querySuoDingData = id;
                    var buttonId = "QuerySuoDing_" + querySuoDingData.field + "_" + querySuoDingData.judge;
                    var querySuoDingHtml = layui.kaGridTopButton.getQuerySuoDingHtml(querySuoDingData, 3);
                    if ($("#" + buttonId).length) {
                    } else {
                        $("#querySuoDing_" + querySuoDingData.tableId).append(querySuoDingHtml)
                    }
                    layui.kaGridSearch.init('kaDataGridId');
                }
            }
            this.getQuerySuoDingHtml = function (data, type) {
                var disabled = ``;
                var icon = ``;
                if (type == 1) {
                    disabled = `layui-btn-primary layui-btn-disabled`;
                    icon = `<i class="icon Kaultraiconfont Kaultraicon-lock"></i>`;
                } else if (type == 2) {
                    disabled = `layui-btn-primary layui-border-green`;
                } else if (type == 3) {
                    disabled = `layui-btn-primary layui-border-orange`;
                    icon = `<i class="icon Kaultraiconfont Kaultraicon-lock"></i>`;
                }
                var html = `
                    <button id="QuerySuoDing_` + data.field + "_" + data.judge + `" QuerySuoDing="` + type + `" type="button"  class="layui-btn layui-btn-xs QuerySuoDing_` + type + " " + disabled + `">
                        <input type="hidden" id="` + data.field + `" name="` + data.field + `" value="` + data.value + `" class="layui-input">
                        <input type="hidden" id="` + data.field + `KaJudge" name="` + data.field + `KaJudge" value="` + data.judge + `" class="layui-input">
                        <span>
                            <span>
                                ` + icon + `
                                <span style="color: green;">` + data.name + `</span>
                                <span class="kaGridTopButtonQuerySuoDing">` + layui.kaGridTopButton.getQueryJudgex(data.judge) + `</span>
                                <span style="color: green;">` + data.value + `</span>
                            </span>
                        </span>
                    </button>
                `;
                return html;
            }
            this.getQueryHtml = function (data) {
                var judgeList = layui.kaGridTopButton.getQueryJudge(data.fieldtype);
                // 加載判斷
                if (data.fieldtype == undefined || data.fieldtype == null || data.fieldtype == "") {
                    data.fieldtype = "text";
                }
                var judgeListHtml = ``;
                for (var key in judgeList) {
                    var selected = ``;
                    if (key == data.judge) {
                        selected = `selected`;
                    }
                    judgeListHtml = judgeListHtml + `<option value="` + key + `" ` + selected + `>` + judgeList[key] + `</option>`;
                }
                var html = `
                    <div class="layui-col-md3">
                        <div class="layui-form-item" style="margin-bottom: 0;">
                            <label class="layui-form-label">` + data.name + `</label>
                            <div class="layui-input-block">
                                <div class="layui-row">
                                    <div class="layui-col-xs4 kaGridTopFindQueryJudge">
                                        <select id="` + data.field + `KaJudge" name="` + data.field + `KaJudge" fieldtype="` + data.fieldtype + `" lay-filter="` + data.field + `KaJudge" lay-search="">
                                            ` + judgeListHtml + `
                                        </select>
                                    </div>
                                    <div class="layui-col-xs8">
                                        <input type="text" id="` + data.field + `_value" name="` + data.field + `" fieldtype="` + data.fieldtype + `" value="` + (data.value != undefined && data.value != null && data.value != "" ? data.value : "") + `" placeholder="请输入" class="layui-input">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                `;
                return html;
            }
            this.getQueryJudgex = function (judge) {
                var list = {
                    "kaInclude": "包含",
                    "kaEqual": "等于",
                    "kaNoEqual": "不等于",
                    "kaNOInclude": "不包含",
                    "kaLessThan": "小于",
                    "kaLessEqual": "小于等于",
                    "kaGreater": "大于",
                    "kaGreaterEqual": "大于等于",
                    "kaIsNull": "是NULL",
                    "kaNotIsNull": "不是NULL",
                    "kaIsEmpty": "是空",
                    "kaNotIsEmpty": "不是空",
                    "kaToday": "今天",
                    "kaYesterday": "昨天",
                    "kaTomorrow": "明天",
                    "kaLastWeek": "上周",
                    "kaThisWeek": "本周",
                    "kaNextWeek": "下周",
                    "lastMonth": "上个月",
                    "kaThisMonth": "本月",
                    "kaNextMonth": "下个月",
                    "kaMonth": "月",
                    "kaYear": "年",
                    "kaData": "前一个小时"
                }
                return list[judge];
            }
            this.getQueryJudge = function (fieldtype) {
                var judgeData = {};
                var list = {
                    "public": {
                        "kaEqual": "等于",
                        "kaNoEqual": "不等于",
                        "kaLessThan": "小于",
                        "kaLessEqual": "小于等于",
                        "kaGreater": "大于",
                        "kaGreaterEqual": "大于等于",
                    },
                    "text": {
                        "kaInclude": "包含",
                        "kaNOInclude": "不包含",
                        "kaIsNull": "是NULL",
                        "kaNotIsNull": "不是NULL",
                        "kaIsEmpty": "是空",
                        "kaNotIsEmpty": "不是空"
                    },
                    "month": {
                        "kaMonth": "月"
                    },
                    "year": {
                        "kaYear": "年"
                    },
                    "date": {
                        "kaToday": "今天",
                        "kaYesterday": "昨天",
                        "kaTomorrow": "明天",
                        "kaLastWeek": "上周",
                        "kaThisWeek": "本周",
                        "kaNextWeek": "下周",
                        "lastMonth": "上个月",
                        "kaThisMonth": "本月",
                        "kaNextMonth": "下个月"
                    },
                    "dateTime": {
                        "kaData": "前一个小时"
                    }
                }
                if (fieldtype == "dateTime") {
                    for (var key in list["date"]) {
                        judgeData[key] = list["date"][key]
                    }
                    for (var key in list[fieldtype]) {
                        judgeData[key] = list[fieldtype][key]
                    }
                } else if (fieldtype == "dateStr") {
                    for (var key in list["date"]) {
                        judgeData[key] = list["date"][key]
                    }
                    for (var key in list["month"]) {
                        judgeData[key] = list["month"][key]
                    }
                    for (var key in list["year"]) {
                        judgeData[key] = list["year"][key]
                    }
                    for (var key in list[fieldtype]) {
                        judgeData[key] = list[fieldtype][key]
                    }
                } else if (fieldtype == "dateTimeStr") {
                    for (var key in list["date"]) {
                        judgeData[key] = list["date"][key]
                    }
                    for (var key in list["dateTime"]) {
                        judgeData[key] = list["dateTime"][key]
                    }
                    for (var key in list["month"]) {
                        judgeData[key] = list["month"][key]
                    }
                    for (var key in list["year"]) {
                        judgeData[key] = list["year"][key]
                    }
                    for (var key in list[fieldtype]) {
                        judgeData[key] = list[fieldtype][key]
                    }
                } else {
                    for (var key in list[fieldtype]) {
                        judgeData[key] = list[fieldtype][key]
                    }
                }
                for (var key in list["public"]) {
                    judgeData[key] = list["public"][key]
                }
                return judgeData;
            }
            this.findQueryInput = function (value, KaJudge) {
                var fieldtype = layui.$("#" + KaJudge + "_value").attr("fieldtype");
                if (value == "kaIsNull") { // 是NULL
                    layui.$("#" + KaJudge + "_value").attr("readonly", true);
                    layui.$("#" + KaJudge + "_value").val("是NULL");
                } else if (value == "kaNotIsNull") { // 不是NULL
                    layui.$("#" + KaJudge + "_value").attr("readonly", true);
                    layui.$("#" + KaJudge + "_value").val("不是NULL");
                } else if (value == "kaIsEmpty") { // 是空
                    layui.$("#" + KaJudge + "_value").attr("readonly", true);
                    layui.$("#" + KaJudge + "_value").val("是空");
                } else if (value == "kaNotIsEmpty") { // 不是空
                    layui.$("#" + KaJudge + "_value").attr("readonly", true);
                    layui.$("#" + KaJudge + "_value").val("不是空");
                } else if (value == "kaToday") { // 今天
                    layui.laydate.unbind(KaJudge + "_value");
                    layui.$("#" + KaJudge + "_value").attr("readonly", true);
                    var todayStr = layui.util.toDateString(new Date().getTime(), "yyyy-MM-dd");
                    if (fieldtype == "date" || fieldtype == "dateStr") {
                        layui.$("#" + KaJudge + "_value").val(todayStr);
                    } else if (fieldtype == "dateTime" || fieldtype == "dateTimeStr") {
                        layui.$("#" + KaJudge + "_value").val(todayStr + " 00:00:00");
                    }
                } else if (value == "kaYesterday") { // 昨天
                    layui.laydate.unbind(KaJudge + "_value");
                    layui.$("#" + KaJudge + "_value").attr("readonly", true);
                    var yesterday = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
                    var yesterdayStr = layui.util.toDateString(yesterday.getTime(), "yyyy-MM-dd");
                    if (fieldtype == "date" || fieldtype == "dateStr") {
                        layui.$("#" + KaJudge + "_value").val(yesterdayStr);
                    } else if (fieldtype == "dateTime" || fieldtype == "dateTimeStr") {
                        layui.$("#" + KaJudge + "_value").val(yesterdayStr + " 00:00:00");
                    }
                } else if (value == "kaTomorrow") { // 明天
                    layui.laydate.unbind(KaJudge + "_value");
                    layui.$("#" + KaJudge + "_value").attr("readonly", true);
                    var tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
                    var tomorrowStr = layui.util.toDateString(tomorrow.getTime(), "yyyy-MM-dd");
                    if (fieldtype == "date" || fieldtype == "dateStr") {
                        layui.$("#" + KaJudge + "_value").val(tomorrowStr);
                    } else if (fieldtype == "dateTime" || fieldtype == "dateTimeStr") {
                        layui.$("#" + KaJudge + "_value").val(tomorrowStr + " 00:00:00");
                    }
                } else if (value == "kaLastWeek") { // 上周
                    layui.laydate.unbind(KaJudge + "_value");
                    layui.$("#" + KaJudge + "_value").attr("readonly", true);
                    var tomorrow = new Date(new Date().getTime() - 24 * 60 * 60 * 1000 * 7);
                    var tomorrowStr = layui.util.toDateString(tomorrow.getTime(), "yyyy-MM-dd");
                    if (fieldtype == "date" || fieldtype == "dateStr") {
                        layui.$("#" + KaJudge + "_value").val(tomorrowStr);
                    } else if (fieldtype == "dateTime" || fieldtype == "dateTimeStr") {
                        layui.$("#" + KaJudge + "_value").val(tomorrowStr + " 00:00:00");
                    }
                } else if (value == "kaThisWeek") { // 本周
                    layui.laydate.unbind(KaJudge + "_value");
                    layui.$("#" + KaJudge + "_value").attr("readonly", true);
                    var todayStr = layui.util.toDateString(new Date().getTime(), "yyyy-MM-dd");
                    if (fieldtype == "date" || fieldtype == "dateStr") {
                        layui.$("#" + KaJudge + "_value").val(todayStr);
                    } else if (fieldtype == "dateTime" || fieldtype == "dateTimeStr") {
                        layui.$("#" + KaJudge + "_value").val(todayStr + " 00:00:00");
                    }
                } else if (value == "kaNextWeek") { // 下周
                    layui.laydate.unbind(KaJudge + "_value");
                    layui.$("#" + KaJudge + "_value").attr("readonly", true);
                    var tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000 * 7);
                    var tomorrowStr = layui.util.toDateString(tomorrow.getTime(), "yyyy-MM-dd");
                    if (fieldtype == "date" || fieldtype == "dateStr") {
                        layui.$("#" + KaJudge + "_value").val(tomorrowStr);
                    } else if (fieldtype == "dateTime" || fieldtype == "dateTimeStr") {
                        layui.$("#" + KaJudge + "_value").val(tomorrowStr + " 00:00:00");
                    }
                } else if (value == "lastMonth") { // 上个月
                    layui.laydate.unbind(KaJudge + "_value");
                    layui.$("#" + KaJudge + "_value").attr("readonly", true);
                    const firstDayLastMonth = new Date(new Date().getFullYear(), new Date().getMonth() - 1, 1);
                    var tomorrowStr = layui.util.toDateString(firstDayLastMonth.getTime(), "yyyy-MM-dd");
                    if (fieldtype == "date" || fieldtype == "dateStr") {
                        layui.$("#" + KaJudge + "_value").val(tomorrowStr);
                    } else if (fieldtype == "dateTime" || fieldtype == "dateTimeStr") {
                        layui.$("#" + KaJudge + "_value").val(tomorrowStr + " 00:00:00");
                    }
                } else if (value == "kaThisMonth") { // 月份
                    layui.laydate.unbind(KaJudge + "_value");
                    layui.$("#" + KaJudge + "_value").attr("readonly", true);
                    const firstDayLastMonth = new Date(new Date().getFullYear(), new Date().getMonth(), 1);
                    var tomorrowStr = layui.util.toDateString(firstDayLastMonth.getTime(), "yyyy-MM-dd");
                    if (fieldtype == "date" || fieldtype == "dateStr") {
                        layui.$("#" + KaJudge + "_value").val(tomorrowStr);
                    } else if (fieldtype == "dateTime" || fieldtype == "dateTimeStr") {
                        layui.$("#" + KaJudge + "_value").val(tomorrowStr + " 00:00:00");
                    }
                } else if (value == "kaNextMonth") { // 下个月
                    layui.laydate.unbind(KaJudge + "_value");
                    layui.$("#" + KaJudge + "_value").attr("readonly", true);
                    const firstDayLastMonth = new Date(new Date().getFullYear(), new Date().getMonth() + 1, 1);
                    var tomorrowStr = layui.util.toDateString(firstDayLastMonth.getTime(), "yyyy-MM-dd");
                    if (fieldtype == "date" || fieldtype == "dateStr") {
                        layui.$("#" + KaJudge + "_value").val(tomorrowStr);
                    } else if (fieldtype == "dateTime" || fieldtype == "dateTimeStr") {
                        layui.$("#" + KaJudge + "_value").val(tomorrowStr + " 00:00:00");
                    }
                } else if (value == "kaMonth") { // 月
                    layui.laydate.unbind(KaJudge + "_value");
                    layui.$("#" + KaJudge + "_value").attr("readonly", true);
                    layui.laydate.render({
                        elem: "#" + KaJudge + "_value",
                        type: 'month'
                    });
                } else if (value == "kaYear") { // 年
                    layui.laydate.unbind(KaJudge + "_value");
                    layui.$("#" + KaJudge + "_value").attr("readonly", true);
                    layui.laydate.render({
                        elem: "#" + KaJudge + "_value",
                        type: 'year'
                    });
                } else if (value == "kaInclude" || value == "kaEqual" || value == "kaNoEqual" || value == "kaNOInclude" || value == "kaLessThan" || value == "kaLessEqual" || value == "kaGreater" || value == "kaGreaterEqual") { // 是NULL
                    if (fieldtype == "date" || fieldtype == "dateTime" || fieldtype == "dateStr" || fieldtype == "dateTimeStr") {
                        layui.laydate.unbind(KaJudge + "_value");
                        layui.$("#" + KaJudge + "_value").attr("readonly", true);
                        if (fieldtype == "dateTime" || fieldtype == "dateTimeStr") {
                            layui.laydate.render({
                                elem: "#" + KaJudge + "_value",
                                type: 'datetime'
                            });
                        } else {
                            layui.laydate.render({
                                elem: "#" + KaJudge + "_value"
                            });
                        }

                    } else {
                        layui.$("#" + KaJudge + "_value").removeAttr("readonly");
                        layui.$("#" + KaJudge + "_value").val("");
                    }
                }
            }
        };
        exports('kaGridTopButton', kaGridTopButton);
    }
)